﻿using System;
using System.Data;
using System.Data.Common;
using System.Data.OleDb;
using System.Linq;

namespace ZXW.DotNet.Common.DbUtilities
{
    /// <summary>
    /// OleDbHelper
    /// 有关数据库连接的方法
    /// </summary>
    public class OleDbHelper : BaseDbHelper
    {
        public OleDbHelper(string name)
            : base(name)
        {
            FileName = "OleDbHelper.txt";
        }

        public override DbProviderFactory Instance()
        {
            return OleDbFactory.Instance;
        }

        public override string GetDBNow()
        {
            return " Getdate() ";
        }

        public override string GetDBDateTime()
        {
            string commandText = " SELECT " + GetDBNow();
            Open();
            string dateTime = ExecuteScalar(CommandType.Text, commandText, new DbParameter[0]).ToString();
            Close();
            return dateTime;
        }

        public override string SqlSafe(string value)
        {
            value = value.Replace("'", "''");
            return value;
        }

        public override string PlusSign(params string[] values)
        {
            string returnValue = values.Aggregate(string.Empty, (current, t) => current + (t + " + "));
            returnValue = !String.IsNullOrEmpty(returnValue) ? returnValue.Substring(0, returnValue.Length - 3) : " + ";
            return returnValue;
        }

        public override string GetParameter(string parameter)
        {
            return " ? ";
        }

        public override DbParameter MakeInParam(string targetFiled, object targetValue)
        {
            return new OleDbParameter(targetFiled, targetValue);
        }

        public override DbParameter[] MakeParameters(string targetFiled, object targetValue)
        {
            DbParameter[] dbParameters = null;
            if (targetFiled != null && targetValue != null)
            {
                dbParameters = new DbParameter[1];
                dbParameters[0] = MakeInParam(targetFiled, targetValue);
            }
            return dbParameters;
        }

        public override DbParameter[] MakeParameters(string[] targetFileds, object[] targetValues)
        {
            var dbParameters = new DbParameter[0];
            if (targetFileds != null && targetValues != null)
            {
                dbParameters = new DbParameter[targetFileds.Length];
                for (int i = 0; i < targetFileds.Length; i++)
                {
                    if (targetFileds[i] != null && targetValues[i] != null)
                    {
                        dbParameters[i] = MakeInParam(targetFileds[i], targetValues[i]);
                    }
                }
            }
            return dbParameters;
        }

        public override DbParameter MakeParam(string paramName, DbType dbType, int size, ParameterDirection direction,
                                              object value)
        {
            OleDbParameter param = size > 0
                                       ? new OleDbParameter(paramName, (OleDbType) dbType, size)
                                       : new OleDbParameter(paramName, (OleDbType) dbType);

            param.Direction = direction;
            if (!(direction == ParameterDirection.Output && value == null))
                param.Value = value;

            return param;
        }
    }
}